package world.xuewei.controller;

import lombok.extern.slf4j.Slf4j;
import world.xuewei.model.entity.Classify;
import world.xuewei.model.entity.Commodity;
import world.xuewei.model.entity.MerchantInfo;
import world.xuewei.model.entity.R;
import world.xuewei.service.MerchantInfoService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 商户信息表(MerchantInfo)表控制层
 *
 * @author makejava
 * @since 2024-11-15 19:27:49
 */
@RestController
@RequestMapping("/merchantInfo")
@Slf4j
public class MerchantInfoController {
    /**
     * 服务对象
     */
    @Resource
    private MerchantInfoService merchantInfoService;

    /**
     * 随机获取一个商户的详细信息
     */
    @GetMapping("/random")
    public R getRandomMerchant() {
        try {
            // 获取随机商户信息
            MerchantInfo merchantInfo = merchantInfoService.getRandomMerchant();
            if (merchantInfo == null) {
                return R.error().message("暂无商户信息");
            }

            // 获取商品分类
            List<Classify> classifies = merchantInfoService.getMerchantClassifies(merchantInfo.getId());

            // 获取商品列表
            List<Commodity> commodities = merchantInfoService.getMerchantCommodities(merchantInfo.getId());

            return R.ok()
                    .data("merchantInfo", merchantInfo)
                    .data("classifies", classifies)
                    .data("commodities", commodities);

        } catch (Exception e) {
            log.error("获取随机商户详情失败", e);
            return R.error().message("获取商户详情失败");
        }
    }


    /**
     * 获取商户详情(包含商品和分类)
     */
    @GetMapping("/detail")
    public R getMerchantDetail(HttpServletRequest request) {
        try {
            String userIdStr = (String) request.getAttribute("userId");
            Long userId = Long.parseLong(userIdStr);
            // 获取商户信息
            MerchantInfo merchantInfo = merchantInfoService.queryById(userId);

            if (merchantInfo == null) {
                return R.error().message("商户不存在");
            }
            // 获取商品分类
            List<Classify> classifies = merchantInfoService.getMerchantClassifies(merchantInfo.getId());

            // 获取商品列表
            List<Commodity> commodities = merchantInfoService.getMerchantCommodities(merchantInfo.getId());

            return R.ok()
                    .data("classifies", classifies)
                    .data("commodities", commodities);

        } catch (Exception e) {
            log.error("获取商户详情失败", e);
            return R.error().message("获取商户详情失败");
        }
    }


    /**
     * 获取商户上架的所有商品
     */
    @GetMapping("/putAway")
    public R getPutAway(HttpServletRequest request) {
        try {
            String userIdStr = (String) request.getAttribute("userId");
            Long userId = Long.parseLong(userIdStr);
            // 获取商户上架的所有商品
            List<Commodity> commodities = merchantInfoService.getPutAway(userId);

            return R.ok().data("commodities", commodities);
        } catch (Exception e) {
            log.error("获取商户详情失败", e);
            return R.error().message("获取商户详情失败");
        }
    }

    /**
     * 获取商户下架的所有商品
     */
    @GetMapping("/commoditiesByStatus")
    public R commoditiesByStatus(HttpServletRequest request) {
        try {
            String userIdStr = (String) request.getAttribute("userId");
            Long userId = Long.parseLong(userIdStr);
            // 获取商户信息
            MerchantInfo merchantInfo = merchantInfoService.queryById(userId);

            if (merchantInfo == null) {
                return R.error().message("商户不存在");
            }

            //获取商户的下架的商品
            List<Commodity> commoditiesByStatus =  merchantInfoService.getMerchantCommoditiesByStatus(merchantInfo.getId());

            return R.ok()
                    .data("commoditiesByStatus", commoditiesByStatus);

        } catch (Exception e) {
            log.error("获取商户详情失败", e);
            return R.error().message("获取商户详情失败");
        }
    }





    /**
     * 分页查询
     *
     * @param merchantInfo 筛选条件
     * @param pageRequest  分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<MerchantInfo>> queryByPage(MerchantInfo merchantInfo, PageRequest pageRequest) {
        return ResponseEntity.ok(this.merchantInfoService.queryByPage(merchantInfo, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<MerchantInfo> queryById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(this.merchantInfoService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param merchantInfo 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<MerchantInfo> add(MerchantInfo merchantInfo) {
        return ResponseEntity.ok(this.merchantInfoService.insert(merchantInfo));
    }

    /**
     * 编辑数据
     *
     * @param merchantInfo 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
    public R update(@RequestBody MerchantInfo merchantInfo, HttpServletRequest request) {
        try {
            String userIdStr = (String) request.getAttribute("userId");
            Long userId = Long.parseLong(userIdStr);
            merchantInfo.setUserId(userId);
            merchantInfoService.update(merchantInfo);
            return R.ok().message("修改成功");
        } catch (Exception e) {
            log.error("修改失败：", e);
            return R.error().message("修改失败");
        }

    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Long id) {
        return ResponseEntity.ok(this.merchantInfoService.deleteById(id));
    }

}

